﻿using System;
using System.IO;
using System.Text;

namespace Narwhunderful.Aphelia.Framework.IO.Serialization.Binary
{

    public sealed class BurstBinaryWriter : IBinaryWriter
    {
        private readonly Encoding _encoding;
        private readonly ITypeNameConverter _typeNameConverter;
        private BinaryWriter _writer;

        public BurstBinaryWriter(ITypeNameConverter typeNameConverter, Encoding encoding)
        {
            if (typeNameConverter == null) throw new ArgumentNullException("typeNameConverter");
            if (encoding == null) throw new ArgumentNullException("encoding");
            _encoding = encoding;
            _typeNameConverter = typeNameConverter;
        }

        public void WriteElementId(byte id)
        {
            _writer.Write(id);
        }

        public void WriteNumber(int number)
        {
            BinaryWriterHelpers.WriteNumber(number, _writer);
        }

        public void WriteNumbers(int[] numbers)
        {
            BinaryWriterHelpers.WriteNumbers(numbers, _writer);
        }

        public void WriteType(Type type)
        {
            if (type == null)
            {
                _writer.Write(false);
            }
            else
            {
                _writer.Write(true);
                _writer.Write(_typeNameConverter.ConvertToTypeName(type));
            }
        }


        public void WriteName(string name)
        {
            BinaryWriterHelpers.WriteString(name, _writer);
        }

        public void WriteValue(object value)
        {
            BinaryWriterHelpers.WriteValue(value, _writer);
        }

        public void Open(Stream stream)
        {
            _writer = new BinaryWriter(stream, _encoding);
        }

        public void Close()
        {
            _writer.Flush();
        }

    }
}
